window.addEventListener('load',function () {
    const slider=document.querySelector('.slider-bar')
    const span=slider.querySelectorAll('span')
    const afont=slider.querySelectorAll('.gofont')
    const goBack=document.querySelector('.goBack')
    const sectionpic=document.querySelector('.section-pic')
        span[0].addEventListener('mousemove',function () {
            this.classList.add('gofont')
            this.innerHTML='在线咨询'
        })
        span[0].addEventListener('mouseout', function () {
            this.classList.remove('gofont')
            this.innerHTML='<img src="img/在线咨询.png" alt="">'
        })
        span[1].addEventListener('mousemove', function () {
            this.classList.add('gofont')
            this.innerHTML='手机课堂'
        })
        span[1].addEventListener('mouseout', function () {
            this.classList.remove('gofont')
            this.innerHTML='<img src="img/phon.png" alt="">'
        })
        span[2].addEventListener('mousemove', function () {
            this.classList.add('gofont')
            this.innerHTML='返回顶部'
        })
        span[2].addEventListener('mouseout', function () {
            this.classList.remove('gofont')
            this.innerHTML='<img src="img/top.png" alt="">'
        })
        // 页面滚动事件
        window.addEventListener('scroll',function () {
            console.log(window.pageYOffset);
            // 当我们页面被卷去的头部大于等于100时 侧边栏就要改为固定定位
            if(window.pageYOffset>=sectionpic.offsetTop){
                slider.style.display='block'
                slider.style.position='fixed'
            }else{
                slider.style.position='absolute'
                slider.style.display='none'
            }
                })
        goBack.addEventListener('click',function () {
            animate(window, 0,0)
              });
    function animate(obj, target, callback) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function() {
            var step = (target - window.pageYOffset) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (window.pageYOffset == target) {
                clearInterval(obj.timer);
                // 回调函数写到定时器结束里面
                // if (callback) {
                //     // 调用函数
                //     callback();
                // }
                // 高级写法
                callback && callback();
            }
              window.scroll(0,window.pageYOffset + step)
        }, 15);
    }
})